/**
 * @class   wosh::network::SocketTcp
 * @brief   The class provides the interface of a generic TCP socket.
 *
 ****************************************************************************
 * @version 0.8.377 $Id: SocketTcp.h 2850 2010-08-06 18:49:18Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Network_SocketTcp_H__
 #define __WOSH_Network_SocketTcp_H__

 #include <core/Object.h>
 #include <core/LogContext.h>


namespace wosh {
 namespace network {

 #define _SocketTcp_ERROR_RemoteHostClosedError		1
 #define _SocketTcp_ERROR_HostNotFoundError			2
 #define _SocketTcp_ERROR_ConnectionRefusedError		3

//#define _SocketTcp_DEBUG_ENABLED

class ServerTcp;
class SocketTcp;

class ISocketTcpListener  {

	public:
		virtual void tcpDataAvailable( long data_len, wosh::network::SocketTcp* socket ) = 0;
		virtual void tcpSocketError( int error_code, const char* description, wosh::network::SocketTcp* socket ) = 0;

	public:
		virtual ~ISocketTcpListener() { }

}; // class def




class SocketTcp : public IReflection
 {
	WOSH_CLASSNAME(wosh::network::SocketTcp)
	WOSH_CLASS_ABSTRACT(true)

	public:
		SocketTcp();

		virtual ~SocketTcp() { }

	public:
		virtual WRESULT connect( unsigned long timeout = LIMIT_ULONG_MAX ) = 0;
		virtual WRESULT disconnect( unsigned long timeout = LIMIT_ULONG_MAX ) = 0;
		virtual bool isConnected() const = 0;

		virtual WRESULT setSocket( int socket_descriptor ) = 0;

		virtual int sendDatagram( const char* data, int size = 0 ) = 0;
		int sendDatagram( const std::string& data ) {
			return sendDatagram(data.c_str(), data.size() );
		 }


		virtual int64 read( char *data, int64 maxlen ) = 0;

	//	virtual bool waitWritten( unsigned long timeout = 30000 ) = 0;
	//	virtual bool waitResponse( char* data, int max_size, long& size, unsigned long timeout = 30000 ) = 0;

		virtual bool waitData( unsigned long timeout = LIMIT_ULONG_MAX ) = 0;

		virtual WRESULT connectTo( const std::string& address_port, unsigned long timeout = LIMIT_ULONG_MAX  );
		virtual WRESULT connectTo( const std::string& address, int port_value, unsigned long timeout = LIMIT_ULONG_MAX  );
 

/** @name Getters
 * @{
 ******************************************************************************/
	public:
		inline const std::string& getAddress() const				{ return this->address; }
		inline int getPort() const									{ return this->port; }
		inline const std::string& getAddressPort() const 			{ return this->address_port; }

		virtual std::string getPeerAddressPort() const = 0;

		inline wosh::network::ISocketTcpListener* getListener()		{ return this->listener; }

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		bool setAddressPort( const std::string& add_value, int port_value );
		bool setAddressPort( const std::string& value );

		inline void setListener( wosh::network::ISocketTcpListener* tcp_listener )	{ this->listener = tcp_listener; }

//@}
/** @name Internal vars
 * @{
 ******************************************************************************/
	protected:
		wosh::network::ISocketTcpListener* listener;

		std::string address;		///< IP address
		int port;					///< IP port
		std::string address_port; 	///< shadow var

		friend class wosh::network::ServerTcp;

#ifdef _SocketTcp_DEBUG_ENABLED
		mutable wosh::LogContext Log;
#endif

//@}


}; // class def

 }; // namespace network
}; // namespace wosh

#endif //__WOSH_Network_SocketTcp_H__
